<template>
  <div class="app-container">
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table" :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="name" label="流程名称"/>
      <el-table-column prop="key" label="流程标识"/>
      <el-table-column prop="version" label="流程版本"/>
      <el-table-column :show-overflow-tooltip="true" prop="diagramResourceName" label="流程图片">
        <template slot-scope="scope">
          <el-link :underline="false" target="_blank" type="primary" @click="flowInfoImge(scope.row.id,'image')" >{{ scope.row.diagramResourceName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="部署时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deploymentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-permission="['ADMIN','LEAVE_ALL','LEAVE_CREATE']" size="mini" type="text" @click="startFlow(scope.row.id,scope.row.key)">[发起流程]</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialog" title="流程图" append-to-body top="0" width="85%">
      <img :src="flowInfo">
    </el-dialog>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import { mapGetters } from 'vuex'
import { resourceReadImg } from '@/api/framework/flow/flowProcess'
import { getFormKey } from '@/api/framework/flow/oaFlow'
export default {
  mixins: [initData],
  data() {
    return {
      loading: false,
      dialog: false,
      delAllLoading: false,
      flowInfo: ''
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    parseTime,
    beforeInit() {
      this.url = 'flowProcess/list'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    flowInfoImge(procDefId, resType) {
      this.dialog = true
      resourceReadImg(procDefId, resType).then(res => {
        this.flowInfo = 'data:image/png;base64,' + btoa(
          new Uint8Array(res)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        )
      }).catch(err => {
        this.dialog = false
      })
    },
    startFlow(procDefId, key) {
      getFormKey(procDefId, key).then(res => {
        this.$router.push({
          path: res
        })
      }).catch(err => {
      })
    }
  }
}
</script>

<style scoped>

</style>
